package com.stormragetech.sylwanas.demo.spring.config;

import com.stormragetech.sylwanas.process.MQOperationExector;
import com.stormragetech.sylwanas.util.Assert;
import lombok.extern.log4j.Log4j2;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * description: //TODO
 *
 * @author: Xue Bin
 * @version: 1.0.0-SNAPSHOT
 */
@Log4j2
@Configuration
public class RabbitConfig {

    private final ApplicationContext context;

    public RabbitConfig(ApplicationContext context) {
        Assert.notNull(context,"ApplicationContext cannot be null !!!");
        this.context = context;
    }

//    @Bean
//    ConnectionFactory connectionFactory() {
//        Properties properties = new Properties();
//
//        try {
//            Resource res = new ClassPathResource("conf/config.properties");
//            properties.load(res.getInputStream());
//        } catch (IOException e) {
//            throw new RuntimeException("Failed to load config.properties!");
//        }
//
//        String ip = properties.getProperty("rabbit.ip");
//        int port = Integer.valueOf(properties.getProperty("rabbit.port"));
//        String userName = properties.getProperty("rabbit.username");
//        String password = properties.getProperty("rabbit.password");
//
//        CachingConnectionFactory connectionFactory = new CachingConnectionFactory(ip, port);
//
//        connectionFactory.setUsername(userName);
//        connectionFactory.setPassword(password);
//        connectionFactory.setPublisherConfirms(true); // enable confirm mode
//        //connectionFactory.getRabbitConnectionFactory().setAutomaticRecoveryEnabled(true);
//
//        return connectionFactory;
//    }

    @Bean
    MQOperationExector mqOperationExector() {
        MQOperationExector exector = new MQOperationExector("conf/rabbit.yaml");
        try {
            exector.init(context);
            return exector;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

}
